package com.baseManage.core.taglib;

import com.jfinal.template.io.Writer;
import java.util.Map;

import com.baseManage.model.SysRes;
import com.baseManage.model.SysUser;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.stat.ParseException;
import com.jfinal.template.stat.Scope;

public class MenuDirective extends Directive {

	private String userId = "";// 用户id

	@Override
	public void exec(Env env, Scope scope, Writer writer) {
		// 表达式必须通过exprList.eval(scope)求值，返回的数据形式与前端传递的数据格式一致
		Map<String, Object> params = (Map<String, Object>) exprList.eval(scope);
		if ( params.get("userId") == null) {
			throw new ParseException("请传入userId属性", location);
		}
		if (params.get("userId") != null) {
			userId = String.valueOf(params.get("userId"));
		}
		SysUser sysUser = SysUser.dao.findById(userId);

		if (sysUser != null) {
			int uid = sysUser.getInt("id");
			String menuView = SysRes.dao.getSysUserMenuView(uid, "/");
			write(writer, menuView);
		} else {
			write(writer, "");
		}

	}

}
